// 角色管理路由
import { Router } from 'express';
import { authMiddleware } from '@/middlewares/auth.middleware';
import { permissionMiddleware } from '@/middlewares/permission.middleware';
import { dataScopeMiddleware } from '@/middlewares/dataScope.middleware';
import roleController from '@/controllers/system/role.controller';
import { createController } from '@/utils/router/route.controller';
import { 
  validateRoleCreate, 
  validateRoleUpdate, 
  validateRoleRemove, 
  validateRoleDataScope, 
  validateAuthorizeUser,
  validateCancelUserAuthorization,
  validateCancelUserAuthorizationBatch
} from '@/validators/system/role.validator';

const router = createController(Router());

/**
 * 角色管理路由
 * 路径前缀：/system/role
 */

// 所有路由均需认证
router.use(authMiddleware);

// ================== 角色列表相关 ==================
// 获取角色列表
router.get('/list', dataScopeMiddleware(), permissionMiddleware('system:role:list'), roleController.list);

// 获取所有角色列表（下拉）
router.get('/optionselect', permissionMiddleware('system:role:list'), roleController.optionselect);

// ================== 菜单与部门树相关 ==================
// 获取角色菜单树
router.get('/roleMenuTreeselect/:roleId', permissionMiddleware('system:role:query'), roleController.roleMenuTreeselect);

// 获取部门树
router.get('/deptTree', permissionMiddleware('system:role:query'), roleController.deptTreeSelect);

// 根据角色ID查询部门树结构
router.get('/deptTree/:roleId', permissionMiddleware('system:role:query'), roleController.roleDeptTreeselect);

// ================== 权限与数据范围 ==================
// 检查用户权限
router.post('/checkPermission', permissionMiddleware('system:role:query'), roleController.checkPermission);

// 获取用户数据权限范围
router.get('/dataScope/:userId', permissionMiddleware('system:role:query'), roleController.getUserDataScope);

// 角色数据权限
router.put('/dataScope', permissionMiddleware('system:role:edit'), validateRoleDataScope, roleController.dataScope);

// ================== 授权用户相关 ==================
// 查询已分配用户角色列表
router.get('/authUser/allocatedList', permissionMiddleware('system:role:list'), roleController.allocatedList);

// 查询未分配用户角色列表
router.get('/authUser/unallocatedList', permissionMiddleware('system:role:list'), roleController.unallocatedList);

// 保存角色授权用户
router.put('/authUser/selectAll', permissionMiddleware('system:role:edit'), validateAuthorizeUser, roleController.authorizeUser);

// GET方式授权
router.get('/authUser/selectAll', permissionMiddleware('system:role:edit'), validateAuthorizeUser, roleController.authorizeUser);

// 单个取消授权用户
router.put('/authUser/cancel/:roleId/:userId', permissionMiddleware('system:role:edit'), validateCancelUserAuthorization, roleController.cancelUserAuthorization);

// 批量取消授权用户
router.put('/authUser/cancelAll', permissionMiddleware('system:role:edit'), validateCancelUserAuthorizationBatch, roleController.cancelUserAuthorizationBatch);

// ================== 导出相关 ==================
// 导出角色
router.post('/export', permissionMiddleware('system:role:export'), roleController.export);

// ================== 角色详情 ==================
// 获取角色详细信息
router.get('/:roleId', permissionMiddleware('system:role:query'), roleController.getInfo);

// ================== 新增、修改、删除 ==================
// 新增角色
router.POST('', permissionMiddleware('system:role:add'), validateRoleCreate, roleController.add);

// 修改角色
router.PUT('', permissionMiddleware('system:role:edit'), validateRoleUpdate, roleController.update);

// 删除角色
router.DELETE('/:roleIds', permissionMiddleware('system:role:remove'), validateRoleRemove, roleController.remove);

export default router;